package com.digitalchina.lxnsy.viewholder;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.entity.ExpertNewVO;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.facebook.drawee.view.SimpleDraweeView;
import com.hedgehog.ratingbar.RatingBar;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.MessageFormat;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.viewholder
 * 创建人：郑皓
 * 创建时间：2018/1/12 11:45:58
 * 修改备注：
 * ================================================
 */

public class AddMyExpertsListHolder extends BaseViewHolder<ExpertNewVO> {

    private SimpleDraweeView ivImage;//头像
    private LinearLayout select;
    private ImageView chooseImage;//选中状态
    private TextView tvExpertName;//姓名
    private TextView tvExpertTitle;//职称
    private TextView tvExpertArea;//涉及领域
    private RatingBar barScore;//评分

    private TextView tvAskNum;  // 提问量
    private TextView tvReplyNum; // 回复量
    private TextView tvStudentNum; // 学员数
    private TextView address; // 地址
    private AddExpertListener listener;
    private int addCount = 0;


    public AddMyExpertsListHolder(ViewGroup parent, AddExpertListener listener, int addCount) {
        super(parent, R.layout.item_addexpert_list);
        this.listener = listener;
        this.addCount = addCount;
        ivImage = $(R.id.item_iv_expert_iv_avatar);
        select = $(R.id.select_check);
        tvExpertName = $(R.id.item_expert_list_tv_name);
        chooseImage = $(R.id.choose_image);
        tvExpertTitle = $(R.id.item_expert_list_tv_titles);
        tvExpertArea = $(R.id.item_expert_list_tv_area);
        barScore = $(R.id.item_expert_list_bar_score);

        tvAskNum = $(R.id.item_expert_list_tv_ask_num);
        tvReplyNum = $(R.id.item_expert_list_tv_reply_num);
        tvStudentNum = $(R.id.item_expert_list_tv_student_num);
        address = $(R.id.address);
    }

    @Override
    public void setData(final ExpertNewVO data) {
        super.setData(data);

        tvAskNum.setText(MessageFormat.format("提问量：{0}", StringUtil.isStrEmpty(data.getQuestionNUm()) ? "0" : data.getQuestionNUm()));
        tvReplyNum.setText(MessageFormat.format("回复量：{0}", StringUtil.isStrEmpty(data.getAnswerNum()) ? "0" : data.getAnswerNum()));
        tvStudentNum.setText(MessageFormat.format("学员数：{0}", StringUtil.isStrEmpty(data.getStudentNum()) ? "0" : data.getStudentNum()));
        address.setText((data.getProvince() == null?"":data.getProvince().getName()) + (data.getCity() == null?"":data.getCity().getName())
                + (data.getCounty() == null?"":data.getCounty().getName()));

        FrescoUtil.showImageSmall(data.getPhoto() != null ? data.getPhoto() : "", ivImage);

        tvExpertName.setText(!StringUtil.isStrEmpty(data.getExpertName()) ? data.getExpertName() : "匿名专家");

//        if (!TextUtils.isEmpty(data.getExpertSex())) {
//            if (data.getExpertSex().equals("男")) {
//                isMale();
//            } else {
//                isFemale();
//            }
//        }
        if(data.isChecked()) {  //如果选中则取消选中
            chooseImage.setBackgroundResource(R.drawable.add_select_ico);
        }else {
            chooseImage.setBackgroundResource(R.drawable.add_unselect_ico);
        }
        if (data.getExpertTitle() == null || StringUtil.isStrEmpty(data.getExpertTitle().getTitle())) {
            tvExpertTitle.setVisibility(View.GONE);
        } else {
            tvExpertTitle.setVisibility(View.VISIBLE);
            tvExpertTitle.setText(data.getExpertTitle().getTitle());
        }
//        tvExpertArea.setText(data.getExpertClassification() != null ? data.getExpertClassification().getPname() + "  " + data.getExpertClassification().getName() : "");
        // 研究方向
        if (data.getExpertAreas() != null)
        {
            String s = "";
            for (int i = 0; i < data.getExpertAreas().size(); i++)
            {
                s += data.getExpertAreas().get(i).getExpertAreaType().getName() + "-"
                        + data.getExpertAreas().get(i).getExpertAreaDirection().getName() + "  ";
            }
            tvExpertArea.setText(s);
        }
        barScore.setStar((float) data.getScore());

        select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(addCount == 3) {
                    listener.addClick(getAdapterPosition(), false);
                    return;
                }
                if(data.isChecked()) {  //如果选中则取消选中
                    listener.addClick(getAdapterPosition(), true);
                    chooseImage.setBackgroundResource(R.drawable.add_unselect_ico);
                }else {
                    listener.addClick(getAdapterPosition(), true);
                    chooseImage.setBackgroundResource(R.drawable.add_select_ico);
                }
            }
        });
    }

    public interface AddExpertListener {

        void addClick(int pos, boolean canCheck);
    }

}
